package com.puhui.education.activity;


import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.puhui.education.R;
import com.puhui.education.activity.base.BaseFragmentAcy;
import com.puhui.education.fragment.LoginFrag;
import com.puhui.education.fragment.main.CourseFrag;
import com.puhui.education.fragment.main.HomeFrag;
import com.puhui.education.fragment.main.MineFrag;
import com.puhui.education.fragment.main.ShoppingCartFrag;
import com.puhui.education.view.DropDownMenu;
import com.puhui.education.view.TextViewPlus;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainAcy extends BaseFragmentAcy implements DropDownMenu.Filter {
    @Bind(R.id.tvp_showHomeFrag_mainAcy)
    TextViewPlus tvp_showHomeFrag;
    @Bind(R.id.tvp_showCourseFrag_mainAcy)
    TextViewPlus tvp_showCourseFrag;
    @Bind(R.id.tvp_showShoppingCartFrag_mainAcy)
    TextViewPlus tvp_showShoppingCartFrag;
    @Bind(R.id.tvp_showMineFrag_mainAcy)
    TextViewPlus tvp_showMineFrag;
    private FragmentManager fragmentManager;
    private HomeFrag homeFrag;
    private CourseFrag courseFrag;
    private ShoppingCartFrag shoppingCartFrag;
    private MineFrag mineFrag;
    private LoginFrag loginFrag;
    private TextViewPlus textViewPlusSelectedCurrent;
    private Fragment fragmentShowedCurrent;

    private boolean exitFlag = true; // 程序退出标志
    private boolean isSnackBarShow;
    private Snackbar snackbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) { //Pad版提交测试3
        super.onCreate(savedInstanceState);
        setContentView(R.layout.acy_main);
        ButterKnife.bind(this, this);
        initData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.tvp_showHomeFrag_mainAcy, R.id.tvp_showCourseFrag_mainAcy,
            R.id.tvp_showShoppingCartFrag_mainAcy, R.id.tvp_showMineFrag_mainAcy})
    public void onBottomImgClick(View view) {
        switch (view.getId()) {
            case R.id.tvp_showHomeFrag_mainAcy:
                setBottomViewSelected(0);
                showFragment(0);
                break;
            case R.id.tvp_showCourseFrag_mainAcy:
                setBottomViewSelected(1);
                showFragment(1);
                break;
            case R.id.tvp_showShoppingCartFrag_mainAcy:
                setBottomViewSelected(2);
                showFragment(2);
                break;
            case R.id.tvp_showMineFrag_mainAcy:
                setBottomViewSelected(3);
                showFragment(3);
                break;
        }
    }

    @Override
    protected void initData() {
        super.initData();
        fragmentManager = getSupportFragmentManager();
        textViewPlusSelectedCurrent = tvp_showHomeFrag;
        tvp_showHomeFrag.setSelected(true);
        homeFrag = new HomeFrag();
        fragmentManager.beginTransaction().add(R.id.fl_displayFragment_mainAcy, homeFrag).commit();
        fragmentShowedCurrent = homeFrag;
    }

    //更改底部图片状态
    private void setBottomViewSelected(int selected) {
        switch (selected) {
            case 0:
                if (textViewPlusSelectedCurrent == tvp_showHomeFrag) {
                    return;
                } else {
                    tvp_showHomeFrag.setSelected(true);
                    textViewPlusSelectedCurrent.setSelected(false);
                    textViewPlusSelectedCurrent = tvp_showHomeFrag;
                }
                break;
            case 1:
                if (textViewPlusSelectedCurrent == tvp_showCourseFrag) {
                    return;
                } else {
                    tvp_showCourseFrag.setSelected(true);
                    textViewPlusSelectedCurrent.setSelected(false);
                    textViewPlusSelectedCurrent = tvp_showCourseFrag;
                }
                break;
            case 2:
                if (textViewPlusSelectedCurrent == tvp_showShoppingCartFrag) {
                    return;
                } else {
                    tvp_showShoppingCartFrag.setSelected(true);
                    textViewPlusSelectedCurrent.setSelected(false);
                    textViewPlusSelectedCurrent = tvp_showShoppingCartFrag;
                }
                break;
            case 3:
                if (textViewPlusSelectedCurrent == tvp_showMineFrag) {
                    return;
                } else {
                    tvp_showMineFrag.setSelected(true);
                    textViewPlusSelectedCurrent.setSelected(false);
                    textViewPlusSelectedCurrent = tvp_showMineFrag;
                }
                break;
        }
    }

    //显示对应的Fragment
    private void showFragment(int which) {
        switch (which) {
            case 0:
                if (fragmentShowedCurrent == homeFrag) {
                    return;
                } else {
                    fragmentManager.beginTransaction().hide(fragmentShowedCurrent).commit();
                    fragmentManager.beginTransaction().show(homeFrag).commit();
                    fragmentShowedCurrent = homeFrag;
                }
                break;
            case 1:
                if (courseFrag == null) {
                    courseFrag = new CourseFrag();
                    fragmentManager.beginTransaction().hide(fragmentShowedCurrent).commit();
                    fragmentManager.beginTransaction().add(R.id.fl_displayFragment_mainAcy, courseFrag).commit();
                    fragmentShowedCurrent = courseFrag;
                } else {
                    if (fragmentShowedCurrent == courseFrag) {
                        return;
                    } else {
                        fragmentManager.beginTransaction().hide(fragmentShowedCurrent).commit();
                        fragmentManager.beginTransaction().show(courseFrag).commit();
                        fragmentShowedCurrent = courseFrag;
                    }
                }
                break;
            case 2:
                if (shoppingCartFrag == null) {
                    shoppingCartFrag = new ShoppingCartFrag();
                    fragmentManager.beginTransaction().hide(fragmentShowedCurrent).commit();
                    fragmentManager.beginTransaction().add(R.id.fl_displayFragment_mainAcy, shoppingCartFrag).commit();
                    fragmentShowedCurrent = shoppingCartFrag;
                } else {
                    if (fragmentShowedCurrent == shoppingCartFrag) {
                        return;
                    } else {
                        fragmentManager.beginTransaction().hide(fragmentShowedCurrent).commit();
                        fragmentManager.beginTransaction().show(shoppingCartFrag).commit();
                        fragmentShowedCurrent = shoppingCartFrag;
                    }
                }
                break;
            case 3:
                boolean is = false;
                if (is) {
                    if (loginFrag == null) {
                        loginFrag = new LoginFrag();
                        fragmentManager.beginTransaction().hide(fragmentShowedCurrent).commit();
                        fragmentManager.beginTransaction().add(R.id.fl_displayFragment_mainAcy, loginFrag).commit();
                        fragmentShowedCurrent = loginFrag;
                    } else {
                        if (fragmentShowedCurrent == loginFrag) {
                            return;
                        } else {
                            fragmentManager.beginTransaction().hide(fragmentShowedCurrent).commit();
                            fragmentManager.beginTransaction().show(loginFrag).commit();
                            fragmentShowedCurrent = loginFrag;
                        }
                    }
                } else {
                    if (mineFrag == null) {
                        mineFrag = new MineFrag();
                        fragmentManager.beginTransaction().hide(fragmentShowedCurrent).commit();
                        fragmentManager.beginTransaction().add(R.id.fl_displayFragment_mainAcy, mineFrag).commit();
                        fragmentShowedCurrent = mineFrag;
                    } else {
                        if (fragmentShowedCurrent == mineFrag) {
                            return;
                        } else {
                            fragmentManager.beginTransaction().hide(fragmentShowedCurrent).commit();
                            fragmentManager.beginTransaction().show(mineFrag).commit();
                            fragmentShowedCurrent = mineFrag;
                        }
                    }
                }
                break;
        }
    }

    @Override
    public void onMenuItemSelectCompleted(String grade, String subject, String version) {
        courseFrag.getData();

    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exitApp();
        }
        return true;
    }
    private void exitApp() { // 实现再按一次退出程序
        if(snackbar==null){
            snackbar =Snackbar.make(findViewById(R.id.pll_mainAcy), "再按一次退出", Snackbar.LENGTH_SHORT);
            View view = snackbar.getView();
            ((TextView) view.findViewById(R.id.snackbar_text)).setTextColor(getResources().getColor(R.color.white));
            snackbar.setCallback(new Snackbar.Callback() {
                @Override
                public void onDismissed(Snackbar snackbar, int event) {
                    super.onDismissed(snackbar, event);
                    isSnackBarShow = false;
                }

                @Override
                public void onShown(Snackbar snackbar) {
                    super.onShown(snackbar);
                    isSnackBarShow = true;
                }
            });
        }
        if (exitFlag) {
            exitFlag = false;
            snackbar.show();
        } else {
            if (isSnackBarShow) {
                finish();
            } else {
                exitFlag = true;
                exitApp();
            }
        }
    }
}
